<?php Temp::Tinclude("commom/header");?>
<?php Temp::Tinclude("commom/comsearch");?>
<div class="jpform margin_top20">
    <div class="row">
        <div class="col-sm-12 col-md-12">
            <div class="thumbnail">
                <img src="<?php echo Common::getThumb($goods["image"],"300x300");?>" alt="">
                 <ul class="list-group">
                     <li class="list-group-item"><h3>商品名称:<?php echo $goods["base_name"];?></h3></li>
                     <li class="list-group-item"><span class="glyphicon glyphicon-flag"></span>价格:<?php echo $goods["sale_price"];?>(元/斤)</li>
                    <li class="list-group-item"><span class="glyphicon glyphicon-list-alt"></span>所属分类:<?php echo $goods["cate_name"];?></li>
                    <li class="list-group-item"><span class="glyphicon glyphicon-heart"></span>销量:<?php echo $goods["order_num"];?></li>
                     <li class="list-group-item"><span class="glyphicon glyphicon-star"></span>卖出:<?php echo $goods["sale_weight"];?>斤</li>
                    <li class="list-group-item"><button type="button" class="btn btn-primary btn-block" data-toggle="modal" data-target="#exampleModal" data-gid="<?php echo strip_tags($goods["id"]);?>" data-saleprice="<?php echo strip_tags($goods["sale_price"]);?>">下单</button></li>
                    <li class="list-group-item"><p><?php echo $goods["desc"];?></p></li>
                  </ul>
            </div>
        </div>
        
    </div>
</div>



<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
        <h4 class="modal-title" id="exampleModalLabel">加入购物车</h4>
      </div>
      <div class="modal-body">
        <form>
            <input type="hidden" name="gid" id="gid"  />
           <div class="form-group">
            <label for="recipient-name" class="control-label">单价:</label>
            <div class="input-group">
            <input type="text" class="form-control" id="saleprice">
            <div class="input-group-addon">(元/斤)</div>
            </div>
          </div>
          <div class="form-group">
            <label for="recipient-name" class="control-label">重量:</label>
            <div class="input-group">
            <input type="text" class="form-control" placeholder="0.00" id="weight">
            <div class="input-group-addon">(斤)</div>
            </div>
          </div>
        </form>
          <p class="text-success jpfont">结算：<span id="dj">0.00</span>x<span id="zl">0.00</span>=<span id="zj">0.00</span></p>
      </div>
      <div class="modal-footer">
        
        <button type="button" id="addcart" class="btn btn-primary">加入购物车</button>
        <button type="button" class="btn btn-default" data-dismiss="modal" id="close">关闭</button>
      </div>
    </div>
  </div>
</div>





<script>
    $('#exampleModal').on('show.bs.modal', function (event) {
          var modal = $(this);
         var objweight=modal.find('#weight');
         var span_dj=modal.find('#dj');
         var span_zl=modal.find('#zl');
         var span_zj=modal.find('#zj');
         var btn=modal.find('#addcart');
         var objsaleprice=modal.find('#saleprice');
         var objgid=modal.find('#gid');
  var button = $(event.relatedTarget);
  var saleprice = button.data('saleprice') ;
  var gid = button.data('gid') ;

  objgid.val(gid);
  objsaleprice.val(saleprice);
  objweight.val("");
  span_dj.html("0.00");
  span_zl.html("0.00");
  span_zj.html("0.00");

  objsaleprice.blur(function(){
     jisuan(modal);
  });
  objweight.blur(function(){
      jisuan(modal);
  });
  btn.unbind("click").click(function(){
         var dj=parseFloat(objsaleprice.val());
         var _gid=objgid.val();
         var zl=0.0;
         if(objweight.val()){
           zl=parseFloat(objweight.val());
         }
         if(isNaN(dj)){
             alert("请输入正确的单价");
             objsaleprice.focus();
             return false;
         }
         if(isNaN(zl)){
             alert("请输入正确的重量");
             objweight.focus();
             return false;
         }else if(zl==0){
             alert("请输入重量");
             objweight.focus();
             return false;
         }
       $.post("<{echo Common::RewriteUrl("Home","public_ajax_add_cart");}>",{"gid":_gid,"zl":zl,"dj":dj},function(data){
           if(data.status==1){
               $("#close").click();
               shansuo();
               $("#cartcount").html(data.cart_count);
           }else{
               alert("参数异常!");
               
           }
       },"json");
      
  }); 
  
});
function shansuo(){
    var t=window.setInterval(function(){
        $("#cartcount").fadeOut();
        $("#cartcount").fadeIn();
         $("#cartcount").fadeOut();
        $("#cartcount").fadeIn();
         $("#cartcount").fadeOut();
        $("#cartcount").fadeIn();
        window.clearInterval(t);
    },1000);
}

    function jisuan(modal){
         var weight=modal.find('#weight');
         var span_dj=modal.find('#dj');
         var span_zl=modal.find('#zl');
         var span_zj=modal.find('#zj');
         span_dj.html("");
         span_zl.html("");
         span_zj.html("");
         var saleprice=modal.find('#saleprice');
         var dj=parseFloat(modal.find('#saleprice').val());
         var zl=0.0;
         if(weight.val()){
           zl=parseFloat(weight.val());
         }
         if(isNaN(dj)){
             alert("请输入正确的单价");
             saleprice.focus();
         }
         if(isNaN(zl)){
             alert("请输入正确的重量");
             weight.focus();
         }
         span_dj.html(dj+"（元/斤）");
         span_zl.html(zl+"（元）");
         span_zj.html((dj*zl).toFixed(2)+"元");
    }

        

</script>
<?php Temp::Tinclude("commom/footer");?>